package com.zzb.framework.frameworkcontroller.user;

import com.zzb.framework.common.BasePage;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.ServiceException;
import com.zzb.framework.frameworkcontroller.common.FrameworkBaseController;
import com.zzb.framework.role.entity.IUser;
import com.zzb.framework.user.entity.User;
import com.zzb.framework.user.form.UserSearchForm;
import com.zzb.framework.user.service.UserService;
import com.zzb.framework.user.vo.UserRoleVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 用户账号controller
*/
@Controller
@RequestMapping("/framework/user")
public class FrameworkUserController extends FrameworkBaseController {
    private static final Logger logger = LoggerFactory.getLogger(FrameworkUserController.class);
    @Autowired
    private UserService userService;

    /**用户账号管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "framework/user/user/index";
    }

    /**用户账号列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<UserRoleVO> list(UserSearchForm userSearchForm){
        return userService.queryUserVOPage(userSearchForm);
    }

    /**用户账号表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        User user;
        if(StringUtils.isEmpty(id)){
            user = new User();
        } else{
            user = userService.get(id);
            if(user == null) user = new User();
        }
        model.addAttribute("user",user);
        return "framework/user/user/form";
    }

    /**用户账号保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<User> save(User user , HttpServletRequest request){
        try {
            IUser currentUser = getCurrentUser(request);
            return userService.saveUser(user, currentUser);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**用户账号删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return userService.deleteUser(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**用户账号开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return userService.recoveryById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**用户账号屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return userService.trashById(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
